﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// Simple Service Administration Tool for WinNT based systems.
// Copyright © 2010 - Silvan Gehrig
// 
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
// 
// Author:
//  Silvan Gehrig
//
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.ComponentModel;
using SSATool.BL.DM;
using SSATool.Common.Util;

#endregion

namespace SSATool.UI.ViewModel
{
    /// <summary>
    /// Represents the data connection to a service backend system.
    /// </summary>
    public class DataConnection : INotifyPropertyChanged
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private readonly Connection _connection;
        private bool _eventAdded;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Returns true if a connection is established.
        /// </summary>
        public bool IsConnected
        {
            get { return _connection.IsConnected; }
        }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Initializes a new instance of the <see cref="DataConnection"/> class.
        /// </summary>
        internal DataConnection(Connection connection)
        {
            PreCondition.AssertNotNull(connection, "connection");

            _connection = connection;
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Connects to the underlying data service.
        /// </summary>
        /// <param name="destination">Specifies the destination host address.</param>
        /// <param name="password">Specifies the user name of the destination host which is used to log in.</param>
        /// <param name="username">Specifies the password of the destination host which is used to log in.</param>
        /// <param name="callback">Specifies the callback method which is used the operation has been completed.</param>
        public void Connect(string destination, string username, string password, Action<Exception> callback)
        {
            _connection.Connect(destination, username, password, callback);
        }

        /// <summary>
        /// Disconnects from the underlying data service.
        /// </summary>
        public void Disconnect()
        {
            _connection.Disconnect();
        }

        private void EnsureEventAdded()
        {
            if (!_eventAdded)
            {
                _connection.PropertyChanged += OnConnectionPropertyChanged;
                _eventAdded = true;
            }
        }

        private void EnsureEventRemoved()
        {
            if (_onPropertyChanged != null && _eventAdded)
            {
                _connection.PropertyChanged += OnConnectionPropertyChanged;
                _eventAdded = false;
            }
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        private PropertyChangedEventHandler _onPropertyChanged;

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged
        {
            add
            {
                _onPropertyChanged += value;
                EnsureEventAdded();
            }
            remove
            {
                _onPropertyChanged -= value;
                EnsureEventRemoved();
            }
        }

        /// <summary>
        /// Fires the PropertyChanged event.
        /// </summary>
        /// <param name="args">Event args to pass to the receivers.</param>
        protected void OnPropertyChanged(PropertyChangedEventArgs args)
        {
            if (_onPropertyChanged != null)
                _onPropertyChanged(this, args);
        }

        private void OnConnectionPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            OnPropertyChanged(e);
        }

        #endregion
    }
}
